import Promise from 'promise';
import validator from 'validator';
import * as ResultMsg from '../constrants/ResultMsg';
import { errorJson, successJson } from '../common/response';
import { checkId, checkDoc, checkDocs } from '../common/check';
import { ZoneProxy, MenuProxy } from '../proxy';

export {
  getFullZones,
  getZones,
  add,
  del,
  update,
};


async function getFullZones(req, res, next) {
  const appId= res.locals.appId;
  const all = req.body.all || false;
  const conditions = { deleted: false };
  if (!all) {
    conditions.enable = true;
  }

  const options = { sort: '-weight' };

  try {
    const zones = await ZoneProxy.find(appId, conditions, options);

    for (let i = 0; i < zones.length; i++) {
      let conditions = { zoneId: zones[i]._id, deleted: false };
      const menus = await MenuProxy.find(appId, conditions, options);
      zones[i] = zones[i].toObject();
      zones[i].menus = menus;
    }

    successJson(req, res, { data: zones });
  } catch (err) {
    next(err);
  }
}

async function getZones(req, res, next) {
  const appId = res.locals.appId;
  const all = req.body.all || false;

  const conditions = { deleted: false };

  if (!all) {
    conditions.enable = true;
  }

  const opt = { sort: '-weight' };

  try {
    const docs = await ZoneProxy.find(appId, conditions, opt);
    successJson(res, res, { data: docs });
  } catch (err) {
    next(err);
  }
}

async function add(req, res, next) {
  const appId = res.locals.appId;
  const key = validator.trim(req.body.key || '');
  const value = validator.trim(req.body.value || '');

  const conditions = { $or: [{ key }, { value }] };

  try {
    if (key === '' || value === '') {
      return errorJson(req, res, { err: ResultMsg.PARAMS_ERROR });
    }

    const doc = await ZoneProxy.findOne(appId, conditions);
    if (doc) return errorJson(req, res, { err: ResultMsg.REPEAT_DATA });
    const result = await ZoneProxy.create(appId, key, value);
    successJson(req, res, { data: result, msg: ResultMsg.ADDED_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function del(req, res, next) {
  const appId = res.locals.appId;
  const zoneId = req.params.zid;

  const data = {
    deleted: true,
  };

  try {
    await checkId(zoneId);
    await ZoneProxy.update(appId, zoneId, data);
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function update(req, res, next) {
  const appId = res.locals.appId;
  const zoneId = req.params.zid;
  const enable = req.body.enable || false;
  const key = req.body.key || '';
  const value = req.body.value || '';
  const weight = req.body.weight || 0;
  const mustReview = req.body.mustReview || false;
  const template = req.body.template || '';
  const icon = validator.trim(req.body.icon || '');
  const createText = validator.trim(req.body.createText || '');
  const audioAttachment = req.body.audioAttachment || false;

  const data = {
    enable,
    key,
    value,
    weight,
    mustReview,
    template,
    icon,
    createText,
    audioAttachment,
  };

  try {
    await checkId(zoneId);
    await ZoneProxy.update(appId, zoneId, data);
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}
